package com.jymj.service.impl;

import com.jymj.common.annotation.EnviorSupvs;
import com.jymj.dao.EnviorFileDao;
import com.jymj.dao.EnviorSupvsDao;
import com.jymj.dao.EnviorSupvsLogDao;
import com.jymj.entity.*;
import com.jymj.entity.dto.*;
import com.jymj.entity.enviorFile.QueryParamDto;
import com.jymj.entity.fhtnty.EnviorFileFhEntity;
import com.jymj.entity.fhtnty.HomeEntity;
import com.jymj.entity.fhtnty.ScoreFhEntity;
import com.jymj.entity.landmanage.*;
import com.jymj.entity.model.PointAndTypeEntity;
import com.jymj.entity.vo.ComplaintsVo;
import com.jymj.entity.vo.QuestionCount;
import com.jymj.entity.zjd.ZjdCountEntity;
import com.jymj.enumEntity.envior.CnwEnum;
import com.jymj.enumEntity.envior.HjzzsjEnum;
import com.jymj.service.EnviorFileService;
import com.jymj.service.EnviorSupvsService;
import com.jymj.service.LandService;
import com.jymj.utils.RRException;
import com.jymj.utils.ShiroUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static org.apache.commons.io.FileUtils.copyFile;

@Service
@Transactional
public class EnviorSupvsServiceImpl implements EnviorSupvsService {
    @Autowired
    private EnviorSupvsDao enviorSupvsDao;
    @Autowired
    private LandService landService;
    @Autowired
    private EnviorFileService enviorFileService;
    @Autowired
    private EnviorSupvsLogDao enviorSupvsLogDao;
    @Autowired
    private EnviorFileDao enviorFileDao;

    @Override
    public PjEnviorSupvsEntity queryBypoint(String point) {
        return enviorSupvsDao.queryBypoint(point);
    }

    @Override
    public List<PjEnviorSupvsEntity> queryList(EnviorSupvsListDto enviorSupvsListDto) {
        return enviorSupvsDao.queryList(enviorSupvsListDto);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return enviorSupvsDao.queryTotal(map);
    }

    @Override
    @EnviorSupvs(operation = "INSERT")
    @Transactional(rollbackFor = RuntimeException.class)
    public Long save(PjEnviorSupvsEntity pjEnviorSupvsEntity) {
        enviorSupvsDao.save(pjEnviorSupvsEntity);
        Long id = pjEnviorSupvsEntity.getId().longValue();
        return id;
    }

    @Override
    @EnviorSupvs(operation = "DELETE")
    public void delete(Integer id) {
        enviorSupvsDao.delete(id);
    }

    @Override
    public void update(PjEnviorSupvsEntity pjEnviorSupvsEntity) {
        enviorSupvsDao.update(pjEnviorSupvsEntity);
    }

    @Override
    @EnviorSupvs(operation = "DELETEBATCH")
    public void deleteBatch(Long[] ids) {
        enviorSupvsDao.deleteBatch(ids);
    }

    @Override
    public List<KeyValueEntity> queryListByqutype() {
        return enviorSupvsDao.queryListByqutype();
    }

    @Override
    public List<KeyValueEntity> oneMonthList() {
        return enviorSupvsDao.oneMonthList();
    }

    @Override
    public List<KeyValueEntity> threeMonthList() {
        return enviorSupvsDao.threeMonthList();
    }

    @Override
    public List<KeyValueEntity> sixMonthList() {
        return enviorSupvsDao.sixMonthList();
    }

    @Override
    public boolean queryIstrue(String point) {
        return landService.queryIstrue(point);
    }

    @Override
    public List<PjEnviorSupvsEntity> queryByYlPoint(String geometry) {
        return enviorSupvsDao.queryByYlPoint(geometry);
    }

    @Override
    public ZjdCountEntity queryAllCount(String point) {
        return enviorSupvsDao.queryAllCount(point);
    }

    @Override
    public List<KeyValueEntity> queryType(Map<String, Object> params) {
        return enviorSupvsDao.queryType(params);
    }

    @Override
    @Transactional(readOnly = true)
    public List<KeyValueEntity> queryByCun(CunZhenDto cunZhenDto) {
        List<KeyValueEntity> keyValueEntities = enviorSupvsDao.queryByCun(cunZhenDto);
        BigDecimal object2 = new BigDecimal(0);
        BigDecimal object3 = new BigDecimal(0);
        BigDecimal object4 = new BigDecimal(0);
        BigDecimal object6 = new BigDecimal(0);
        BigDecimal object7 = new BigDecimal(0);
        BigDecimal object8 = new BigDecimal(0);
        BigDecimal object9 = new BigDecimal(0);
        BigDecimal object10 = new BigDecimal(0);
        BigDecimal object11 = new BigDecimal(0);
        Long object12 = 0L;
        for (KeyValueEntity keyValueEntity : keyValueEntities) {
            object2 = object2.add(new BigDecimal(keyValueEntity.getObject2().toString()));
            object3 = object3.add(new BigDecimal(keyValueEntity.getObject3().toString()));
            object4 = object4.add(new BigDecimal(keyValueEntity.getObject4().toString()));
            object6 = object6.add(new BigDecimal(keyValueEntity.getObject6().toString()));
            object7 = object7.add(new BigDecimal(keyValueEntity.getObject7().toString()));
            object8 = object8.add(new BigDecimal(keyValueEntity.getObject8().toString()));
            object9 = object9.add(new BigDecimal(keyValueEntity.getObject9().toString()));
            object11 = object11.add(new BigDecimal(keyValueEntity.getObject11().toString()));
            object10 = object10.add(new BigDecimal(keyValueEntity.getObject10().toString()));
            object12 += Long.valueOf(keyValueEntity.getObject12().toString());
        }
        KeyValueEntity keyValueEntity = new KeyValueEntity();
        keyValueEntity.setObject1("总计");
        keyValueEntity.setObject2(object2);
        keyValueEntity.setObject3(object3);
        keyValueEntity.setObject4(object4);
        keyValueEntity.setObject6(object6);
        keyValueEntity.setObject7(object7);
        keyValueEntity.setObject8(object8);
        keyValueEntity.setObject9(object9);
        keyValueEntity.setObject10(object10);
        keyValueEntity.setObject11(object11);
        keyValueEntity.setObject12(object12);
        keyValueEntities.add(0, keyValueEntity);
        return keyValueEntities;
    }

    @Override
    public List<PointRecordEntity> queryPointList(PointPageDto pointPageDto) {
        return enviorSupvsDao.queryPointList(pointPageDto);
    }

    @Override
    public int queryPointTotal(Map<String, Object> params) {
        return enviorSupvsDao.queryPointTotal(params);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    @EnviorSupvs(operation = "SAVE_POINT")
    public PointRecordEntity savePoint(PointRecordEntity pointRecordEntity) {
        enviorSupvsDao.savePoint(pointRecordEntity);
        List<RenovatedFile> renovatedFiles = pointRecordEntity.getFiles();
        if (renovatedFiles.size() > 0) {
            renovatedFiles.forEach(renovatedFile -> renovatedFile.setMsid(pointRecordEntity.getId().longValue()));
            enviorFileDao.updatePointFileMsid(renovatedFiles, pointRecordEntity.getId().longValue());
        }
        return pointRecordEntity;
    }

    @Override
    @EnviorSupvs(operation = "DELETE_POINT")
    @Transactional(rollbackFor = RuntimeException.class)
    public void deletePoint(Integer[] ids) {
        if (ids.length < 1) throw new RRException("请至少选择一个点位");
        enviorSupvsDao.deletePoint(ids);
        for (Integer id : ids) {
            List<RenovatedFile> files = enviorFileDao.getFiles(null, id.longValue());
            if (files.size() < 1)
                continue;
            files.forEach(file -> new File(file.getPath()).delete());
        }
    }

    @Override
    public PjEnviorSupvsEntity queryInfo(Integer id) {
        return enviorSupvsDao.queryInfo(id);
    }

    @Override
    public void saveRejected(PjEnviorRejectedEntity pjEnviorRejectedEntity) {
        enviorSupvsDao.saveRejected(pjEnviorRejectedEntity);
    }

    @Override
    public void updateBh(PjEnviorSupvsEntity pjEnviorSupvsEntity) {
        enviorSupvsDao.updateBh(pjEnviorSupvsEntity);
    }

    @Override
    public List<PjEnviorRejectedEntity> queryRej(Integer id) {
        return enviorSupvsDao.queryRej(id);
    }

    @Override
    public List<PjEnviorSupvsEntity> queryByCode(Map<String, Object> params) {
        return enviorSupvsDao.queryByCode(params);
    }

    @Override
    public Integer selectScoreByCode(String code) {
        return enviorSupvsDao.selectScoreByCode(code);
    }

    @Override
    public Integer selectScoreByListcode(List<String> codes) {
        return enviorSupvsDao.selectScoreByListcode(codes);
    }

    @Override
    public ScoreFhEntity queryScore(String code) {
        ScoreFhEntity scoreFhEntity = enviorSupvsDao.queryScore(code);
        if (scoreFhEntity != null) {
            ScoreFhEntity scoreFhEntity1 = enviorSupvsDao.qyeruScoreMon(code);
            scoreFhEntity.setHcqtghmon(scoreFhEntity1 == null ? 100 : scoreFhEntity1.getHcqtghmon());
            scoreFhEntity.setHjzzmon(scoreFhEntity1 == null ? 100 : scoreFhEntity1.getHjzzmon());
            scoreFhEntity.setJnldmon(scoreFhEntity1 == null ? 100 : scoreFhEntity1.getJnldmon());
            scoreFhEntity.setLhyhmon(scoreFhEntity1 == null ? 100 : scoreFhEntity1.getLhyhmon());
            scoreFhEntity.setTynysmon(scoreFhEntity1 == null ? 100 : scoreFhEntity1.getTynysmon());
        }
        return scoreFhEntity;
    }


    @Override
    public List<PjEnviorSupvsEntity> queryBygeom(String geome) {
        return enviorSupvsDao.queryBygeom(geome);
    }

    @Override
    public HomeEntity queryHome(ReportDto reportDto) {
        HomeEntity homeEntity = enviorSupvsDao.queryHome(reportDto);
        return homeEntity;
    }

    @Override
    public List<HomeEntity> queryHomeZhen(ReportDto reportDto) {
        return enviorSupvsDao.queryHomeZhen(reportDto);
    }

    @Override
    public List<JournalEntity> queryJournal(String date, Integer type) {
        //查询村
        List<JournalEntity> journalEntities = enviorSupvsDao.queryJournal(date, type);
        //查询问题数量
        List<JournalEntity> journalEntities1 = enviorSupvsDao.queryWts(date, type);
        //查询问题备注
        List<JournalEntity> journalEntities2 = enviorSupvsDao.queryWtbz(date, type);
        for (JournalEntity journalEntity : journalEntities) {
            List<JournalEntity> journalEntityList = new ArrayList<>();
            for (JournalEntity journalEntity1 : journalEntities1) {
                List<JournalEntity> journalEntityList1 = new ArrayList<>();
                if (journalEntity.getCode().equals(journalEntity1.getCode())) {
                    journalEntityList.add(journalEntity1);
                }
                for (JournalEntity journalEntity2 : journalEntities2) {
                    if (journalEntity1.getCode().equals(journalEntity2.getCode())
                            && journalEntity1.getFl().equals(journalEntity2.getFl())) {
                        journalEntityList1.add(journalEntity2);
                    }
                }
                journalEntity1.setJournalEntities(journalEntityList1);
            }
            journalEntity.setJournalEntities(journalEntityList);
        }
        return journalEntities;
    }

    @Override
    public RjhjReport rjhjReport(ReportDto reportDto) {
        List<RjhjReport> rjhjReports = new ArrayList<>();
        if(ShiroUtils.getUserEntity().getType() == 2){
            reportDto.setCode(ShiroUtils.getUserEntity().getCode());
            rjhjReports = enviorSupvsDao.rjhjReportZhen(reportDto);
        }else {
            rjhjReports = enviorSupvsDao.rjhjReport(reportDto);
        }

        Integer zwts = 0;//总问题数
        Integer ljwt = 0;//垃圾问题数
        Integer wswt = 0;//污水问题数
        Integer gcwt = 0;//公厕问题数
        Integer ldlfwt = 0;//乱堆乱放问题数
        Integer ltlhwt = 0;//乱贴乱画问题数
        Integer sdljwt = 0;//私搭乱建问题数
        Integer qtwt = 0;//其他问题数
        Integer yzg = 0;//已整改数
        Integer wzg = 0;//未整改数
        Integer gcwkf = 0;//公厕未开放数
        Integer hcwt = 0;//旱厕问题数
        Integer jflmwt = 0;//街坊路面问题数
        Integer jbjss = 0;//接诉即办

        String zwtszhen = null;//总问题数最多镇
        String ljwtzhen = null;//垃圾问题数最多镇
        String wswtzhen = null;//污水问题数最多镇
        String gcwtzhen = null;//公厕问题数最多镇
        String ldlfwtzhen = null;//乱堆乱放问题数最多镇
        String ltlhwtzhen = null;//乱贴乱画问题数最多镇
        String sdljwtzhen = null;//私搭乱建问题数最多镇
        String qtwtzhen = null;//其他问题数最多镇
        String jbjsszhen = null;//接诉即办问题数最多镇
        String hcwtzhen = null;//旱厕问题数最多镇
        String jflmwtzhen = null;//街坊路面问题数最多镇

        Integer zzwts = 0;//镇总问题数
        Integer zljwt = 0;//镇垃圾问题数
        Integer zwswt = 0;//镇污水问题数
        Integer zgcwt = 0;//镇公厕问题数
        Integer zldlfwt = 0;//镇乱堆乱放问题数
        Integer zltlhwt = 0;//镇乱贴乱画问题数
        Integer zsdljwt = 0;//镇私搭乱建问题数
        Integer zqtwt = 0;//镇其他问题数
        Integer zjbjss = 0;//镇接诉即办问题数
        Integer zhcwt = 0;//镇旱厕问题数
        Integer zjflmwt = 0;//镇街坊路面问题数
        for (RjhjReport rjhjReport : rjhjReports) {
            zwts = zwts + rjhjReport.getZwts();
            ljwt = ljwt + rjhjReport.getLjwt();
            wswt = wswt + rjhjReport.getWswt();
            gcwt = gcwt + rjhjReport.getGcwt();
            ldlfwt = ldlfwt + rjhjReport.getLdlfwt();
            ltlhwt = ltlhwt + rjhjReport.getLtlhwt();
            sdljwt = sdljwt + rjhjReport.getSdljwt();
            qtwt = qtwt + rjhjReport.getQtwt();
            yzg = yzg + rjhjReport.getYzg();
            wzg = wzg + rjhjReport.getWzg();
            hcwt = hcwt + rjhjReport.getHcwt();
            jflmwt = jflmwt + rjhjReport.getJflmwt();
            jbjss = jbjss + rjhjReport.getJbjss();
            gcwkf = gcwkf + rjhjReport.getGcwkf();
            if (rjhjReport.getZwts() != 0 && rjhjReport.getZwts() > zzwts) {
                zwtszhen = rjhjReport.getZhen();
                zzwts = rjhjReport.getZwts();
            }
            if (rjhjReport.getHcwt() != 0 && rjhjReport.getHcwt() > zhcwt) {
                hcwtzhen = rjhjReport.getZhen();
                zhcwt = rjhjReport.getHcwt();
            }
            if (rjhjReport.getJflmwt() != 0 && rjhjReport.getJflmwt() > zjflmwt) {
                jflmwtzhen = rjhjReport.getZhen();
                zjflmwt = rjhjReport.getJflmwt();
            }
            if (rjhjReport.getJbjss() != 0 && rjhjReport.getJbjss() > zjbjss) {
                jbjsszhen = rjhjReport.getZhen();
                zjbjss = rjhjReport.getJbjss();
            }
            if (rjhjReport.getLjwt() != 0 && rjhjReport.getLjwt() > zljwt) {
                ljwtzhen = rjhjReport.getZhen();
                zljwt = rjhjReport.getLjwt();
            }
            if (rjhjReport.getWswt() != 0 && rjhjReport.getWswt() > zwswt) {
                wswtzhen = rjhjReport.getZhen();
                zwswt = rjhjReport.getWswt();
            }
            if (rjhjReport.getGcwt() != 0 && rjhjReport.getGcwt() > zgcwt) {
                gcwtzhen = rjhjReport.getZhen();
                zgcwt = rjhjReport.getGcwt();
            }
            if (rjhjReport.getLdlfwt() != 0 && rjhjReport.getLdlfwt() > zldlfwt) {
                ldlfwtzhen = rjhjReport.getZhen();
                zldlfwt = rjhjReport.getLdlfwt();
            }
            if (rjhjReport.getLtlhwt() != 0 && rjhjReport.getLtlhwt() > zltlhwt) {
                ltlhwtzhen = rjhjReport.getZhen();
                zltlhwt = rjhjReport.getLtlhwt();
            }
            if (rjhjReport.getSdljwt() != 0 && rjhjReport.getSdljwt() > zsdljwt) {
                sdljwtzhen = rjhjReport.getZhen();
                zsdljwt = rjhjReport.getSdljwt();
            }
            if (rjhjReport.getQtwt() != 0 && rjhjReport.getQtwt() > zqtwt) {
                qtwtzhen = rjhjReport.getZhen();
                zqtwt = rjhjReport.getQtwt();
            }
        }

        RjhjReport rjhjReport = new RjhjReport();
        rjhjReport.setZwts(zwts);
        rjhjReport.setLjwt(ljwt);
        rjhjReport.setWswt(wswt);
        rjhjReport.setGcwt(gcwt);
        rjhjReport.setLdlfwt(ldlfwt);
        rjhjReport.setLtlhwt(ltlhwt);
        rjhjReport.setSdljwt(sdljwt);
        rjhjReport.setQtwt(qtwt);
        rjhjReport.setYzg(yzg);
        rjhjReport.setWzg(wzg);
        rjhjReport.setZwtszhen(zwtszhen);
        rjhjReport.setLjwtzhen(ljwtzhen);
        rjhjReport.setWswtzhen(wswtzhen);
        rjhjReport.setGcwtzhen(gcwtzhen);
        rjhjReport.setLdlfwtzhen(ldlfwtzhen);
        rjhjReport.setLtlhwtzhen(ltlhwtzhen);
        rjhjReport.setSdljwtzhen(sdljwtzhen);
        rjhjReport.setQtwtzhen(qtwtzhen);
        rjhjReport.setZzwts(zzwts);
        rjhjReport.setZljwt(zljwt);
        rjhjReport.setZwswt(zwswt);
        rjhjReport.setZgcwt(zgcwt);
        rjhjReport.setZldlfwt(zldlfwt);
        rjhjReport.setZltlhwt(zltlhwt);
        rjhjReport.setZsdljwt(zsdljwt);
        rjhjReport.setZqtwt(zqtwt);
        rjhjReport.setJbjss(jbjss);
        rjhjReport.setZjbjss(zjbjss);
        rjhjReport.setJbjsszhen(jbjsszhen);
        rjhjReport.setHcwt(hcwt);
        rjhjReport.setZhcwt(zhcwt);
        rjhjReport.setHcwtzhen(hcwtzhen);
        rjhjReport.setJflmwt(jflmwt);
        rjhjReport.setZjflmwt(zjflmwt);
        rjhjReport.setJflmwtzhen(jflmwtzhen);
        return rjhjReport;
    }

    @Override
    public ScoreReport scoreReport(String date) {
        return enviorSupvsDao.scoreReport(date);
    }

    @Override
    public List<ScoreMonEntity> bhgcz(String date, Integer type) {
        Map map = new HashMap();
        map.put("date", date);
        map.put("type", type);
        return enviorSupvsDao.bhgcz(map);
    }

    @Override
    public List<JournalEntity> queryDxwt(String date, Integer type) {
        List<JournalEntity> journalEntities = enviorSupvsDao.queryDxwt(date, type);
        Map<Integer, List<JournalEntity>> map = journalEntities.stream().collect(Collectors.groupingBy(
                JournalEntity::getFl, LinkedHashMap::new, Collectors.toList()));
        Object[] array = map.keySet().toArray();
        List<JournalEntity> journalEntities1 = new ArrayList<>();
        for (Object array1 : array) {
            JournalEntity journalEntity = new JournalEntity();
            String proj = array1 + "";
            journalEntity.setFl(Integer.valueOf(proj));
            journalEntity.setJournalEntities(map.get(Integer.valueOf(proj)) == null ?
                    new ArrayList<>() : map.get(Integer.valueOf(proj)));
            journalEntities1.add(journalEntity);
        }
        return journalEntities1;
    }

    @Override
    public List<PjEnviorSupvsEntity> queryByXzqmc(String date, String code, Integer type, Integer[] hjzzsjs) {
        Map map = new HashMap();
        map.put("date", date);
        map.put("code", code);
        map.put("type", type);
        map.put("hjzzsjs", hjzzsjs);
        return enviorSupvsDao.queryByXzqmc(map);
    }

    @Override
    public void saveLog(PjEnviorSupvsLog pjEnviorSupvsLog) {
        enviorSupvsDao.saveLog(pjEnviorSupvsLog);
    }

    @Override
    public List<PjEnviorSupvsLog> enviorLog(String date) {
        return enviorSupvsDao.enviorLog(date);
    }

    @Override
    public PjEnviorSupvsEntity queryHjpj(String point) {
        return enviorSupvsDao.queryHjpj(point);
    }

    @Override
    public List<EnviorSmsEntity> querySms() {
        List<EnviorSmsEntity> enviorSmsEntities = enviorSupvsDao.querySms();
        for (EnviorSmsEntity enviorSmsEntity : enviorSmsEntities) {
            EnviorSmsEntity enviorSmsEntity1 = enviorSupvsDao.queryCount(enviorSmsEntity.getCode());
            enviorSmsEntity.setCountsNew(enviorSmsEntity1.getCountsNew());
            enviorSmsEntity.setCountsYq(enviorSmsEntity1.getCountsYq());
            String[] strings = enviorSupvsDao.queryPhone(enviorSmsEntity.getCode());
            enviorSmsEntity.setPhones(strings.length == 0 ? new String[0] : strings);
        }
        return enviorSmsEntities;
    }

    @Override
    public List<EnviorSmsEntity> querySmsByZhen() {
        List<EnviorSmsEntity> enviorSmsEntities = enviorSupvsDao.querySmsByZhen();
        for (EnviorSmsEntity enviorSmsEntity : enviorSmsEntities) {
            EnviorSmsEntity enviorSmsEntity1 = enviorSupvsDao.queryCount(enviorSmsEntity.getCode());
            enviorSmsEntity.setCountsNew(enviorSmsEntity1.getCountsNew());
            enviorSmsEntity.setCountsYq(enviorSmsEntity1.getCountsYq());
            String[] strings = enviorSupvsDao.queryPhoneByZhen(enviorSmsEntity.getCode());
            enviorSmsEntity.setPhones(strings.length == 0 ? new String[0] : strings);
        }
        return enviorSmsEntities;
    }

    @Override
    public KeyValueEntity countByCode(EnviorSupvsListDto enviorSupvsDto) {
        enviorSupvsDto.setSysUser(ShiroUtils.getUserEntity());
        return enviorSupvsDao.countByCode(enviorSupvsDto);
    }

    @Override
    @EnviorSupvs(operation = "UPDATE_POINT")
    @Transactional(rollbackFor = RuntimeException.class)
    public void updatePoint(PointRecordEntity pointRecordEntity) {
        enviorSupvsDao.updatePoint(pointRecordEntity);
        List<RenovatedFile> renovatedFiles = pointRecordEntity.getFiles();
        if (renovatedFiles.size() != 0) {
            renovatedFiles.forEach(renovatedFile -> renovatedFile.setMsid(pointRecordEntity.getId().longValue()));
            enviorFileDao.updatePointFileMsid(renovatedFiles, pointRecordEntity.getId().longValue());
        }
    }

    @Override
    public PointRecordEntity queryByPoint(String point) {
        return enviorSupvsDao.queryByPoint(point);
    }

    @Override
    public List<PjEnviorSupvsEntity> queryById(Integer gddwid) {
        return enviorSupvsDao.queryById(gddwid);
    }

    @Override
    public List<PjEnviorSupvsEntity> queryByXzq(String point) {
        return enviorSupvsDao.queryByXzq(point);
    }

    @Override
    public List<PjEnviorSupvsEntity> queryListByIds(Integer[] ids) {
        return enviorSupvsDao.queryListByIds(ids);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void updateQutype(PjEnviorSupvsEntity pjEnviorSupvsEntity) {
        if (pjEnviorSupvsEntity == null || pjEnviorSupvsEntity.getQutype() == null || pjEnviorSupvsEntity.getId() == null)
            throw new RRException("参数不能为空");
        int row = enviorSupvsDao.updateQutype(pjEnviorSupvsEntity);
        if (row == 0) throw new RRException("id可能已不存在 修改失败");
    }

    @Override
    public List<EnviorSupvsLogEntity> getEnviorLog(EnviorSupvsLogEntity enviorSupvsLogEntity) {
        return enviorSupvsLogDao.getEnviorLog(enviorSupvsLogEntity);
    }

    @Override
    public void updateCount3() {
        enviorSupvsDao.updateCount3();
    }

    @Override
    public QuestionCount getQuestionCount() {
        QuestionCount questionCount = enviorSupvsDao.getQuestionCount();
        return questionCount;
    }

    @Override
    public List<ComplaintsVo> getZgqk() {
        return enviorSupvsDao.getZgqk();
    }

    @Override
    public List<KeyValueEntity> queryByDj(CunZhenDto cunZhenDto) {
        List<KeyValueEntity> keyValueEntities = enviorSupvsDao.queryByDj(cunZhenDto);
        BigDecimal object2 = new BigDecimal(0);
        BigDecimal object3 = new BigDecimal(0);
        BigDecimal object4 = new BigDecimal(0);
        BigDecimal object6 = new BigDecimal(0);
        BigDecimal object7 = new BigDecimal(0);
        BigDecimal object8 = new BigDecimal(0);
        BigDecimal object9 = new BigDecimal(0);
        BigDecimal object10 = new BigDecimal(0);
        BigDecimal object11 = new BigDecimal(0);
        Long object12 = 0L;
        for (KeyValueEntity keyValueEntity : keyValueEntities) {
            object2 = object2.add(new BigDecimal(keyValueEntity.getObject2().toString()));
            object3 = object3.add(new BigDecimal(keyValueEntity.getObject3().toString()));
            object4 = object4.add(new BigDecimal(keyValueEntity.getObject4().toString()));
            object6 = object6.add(new BigDecimal(keyValueEntity.getObject6().toString()));
            object7 = object7.add(new BigDecimal(keyValueEntity.getObject7().toString()));
            object8 = object8.add(new BigDecimal(keyValueEntity.getObject8().toString()));
            object9 = object9.add(new BigDecimal(keyValueEntity.getObject9().toString()));
            object11 = object11.add(new BigDecimal(keyValueEntity.getObject11().toString()));
            object10 = object10.add(new BigDecimal(keyValueEntity.getObject10().toString()));
            object12 += Long.valueOf(keyValueEntity.getObject12().toString());
        }
        KeyValueEntity keyValueEntity = new KeyValueEntity();
        keyValueEntity.setObject1("总计");
        keyValueEntity.setObject2(object2);
        keyValueEntity.setObject3(object3);
        keyValueEntity.setObject4(object4);
        keyValueEntity.setObject6(object6);
        keyValueEntity.setObject7(object7);
        keyValueEntity.setObject8(object8);
        keyValueEntity.setObject9(object9);
        keyValueEntity.setObject10(object10);
        keyValueEntity.setObject11(object11);
        keyValueEntity.setObject12(object12);
        keyValueEntities.add(0, keyValueEntity);
        return keyValueEntities;
    }



    /**
     * 状态回滚
     *
     * @param id
     * @param qutype
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void fallBackQutype(Integer id, Integer qutype, String comment) {
        //校验
        if (id == null || id <= 0) {
            throw new RRException("id不合法");
        }
//        if (qutype != 0 && qutype != 1 && qutype != 3) {
//            throw new RRException("需要修改的状态不合法");
//        }
//        Integer tempQutype = null;
//        if (qutype == 1)
//            tempQutype = 3;
//        if (qutype == 3)
//            tempQutype = 4;
//        if (qutype == 0) {
//            tempQutype = 1;
//        }
        Integer rows = enviorSupvsDao.fallBackQutype(id, qutype, comment);
        if (rows == 0) {
            throw new RRException("所修改项可能已经不存在或状态不正确");
        }
    }

    @Override
    public PointRecordEntity queryPointInfo(Integer gddwid) {
        return enviorSupvsDao.queryPointInfo(gddwid);
    }

    private final String MONTH_COPY_PATH = "monthCopyFile";
    private final String MONTH_ZIP_PATH = "monthZipFile";

    @Override
    public String getXzqmcByDate(QueryParamDto queryParamDto) throws IOException {
        if (queryParamDto.getType() != 3) {
            if (queryParamDto.getStartDate() == null || queryParamDto.getEndDate() == null) {
                throw new RRException("请选择开始与结束时间", 500);
            }
        } else {
            queryParamDto.setStartDate(null).setEndDate(null);
        }
        List<PjEnviorSupvsEntity> pjEnviorSupvsEntities = enviorSupvsDao.getXzqmcByDate(queryParamDto);
        if (pjEnviorSupvsEntities == null || pjEnviorSupvsEntities.size() == 0) {
            throw new RRException("暂无数据", 500);
        }
        Long userId = ShiroUtils.getUserEntity().getUserId();
        File path = new File(ResourceUtils.getURL("jar:").getPath());
        String fileUpload = path.getAbsolutePath().replace("jar:", "fileupload");
        File toPath = new File(fileUpload + File.separator + MONTH_COPY_PATH + userId);
        FileUtils.deleteDirectory(toPath);


        pjEnviorSupvsEntities.forEach(pjEnviorSupvsEntity -> {
            //获取文件
            File copyFile = new File(fileUpload + File.separator + pjEnviorSupvsEntity.getPath());
//            String picName = pjEnviorSupvsEntity.getPath().substring(pjEnviorSupvsEntity.getPath().lastIndexOf("\\") + 1);
            String picName = FilenameUtils.getName(pjEnviorSupvsEntity.getPath());
            File toFile1 = new File(toPath + File.separator + pjEnviorSupvsEntity.getZhen() + File.separator + pjEnviorSupvsEntity.getXzqmc() + File.separator + HjzzsjEnum.getName(pjEnviorSupvsEntity.getHjzzsj()) + File.separator + CnwEnum.getName(pjEnviorSupvsEntity.getCnw()) + File.separator + picName);
            try {
                copyFile(copyFile, toFile1);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RRException("复制文件可能不存在", e);
            }
        });
        File zipPath = new File(fileUpload + File.separator + MONTH_ZIP_PATH);
        enviorFileService.zipMothPic(toPath, zipPath);
        FileUtils.deleteDirectory(toPath);
        return MONTH_ZIP_PATH + File.separator + "monthCopyFile" + userId + ".zip";
    }

    @Override
    public void quertFile7() {
        List<PjEnviorSupvsEntity> pjEnviorSupvsEntities = enviorSupvsDao.quertFile7();
        String fileurl = null;
        for (PjEnviorSupvsEntity pjEnviorSupvsEntity : pjEnviorSupvsEntities) {
            fileurl =  "E:/rjhj"+ File.separator + pjEnviorSupvsEntity.getZhen() + File.separator
                    + pjEnviorSupvsEntity.getXzqmc() + File.separator  + pjEnviorSupvsEntity.getHjzzsjText();
            File file = new File(fileurl);
            if (!file.exists()){
                file.mkdirs();
            }
            try (InputStream ins = new URL(pjEnviorSupvsEntity.getPath()).openStream()) {
                Path target = Paths.get(fileurl, pjEnviorSupvsEntity.getRemark());
                Files.createDirectories(target.getParent());
                Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void rejected(PjEnviorSupvsEntity pjEnviorSupvsEntity) {
        pjEnviorSupvsEntity.setCreateTime(new Date());
        pjEnviorSupvsEntity.setSqbh(false);
        if (pjEnviorSupvsEntity == null) {
            throw new RRException("参数不能为空");
        }
        if (pjEnviorSupvsEntity.getCode() == null || "".equals(pjEnviorSupvsEntity.getCode())) {
            throw new RRException("村名不能为空");
        }
        if (pjEnviorSupvsEntity.getQutype() > 5 && pjEnviorSupvsEntity.getQutype() != 100
                && pjEnviorSupvsEntity.getQutype() != 101
                && pjEnviorSupvsEntity.getQutype() != 102
                && pjEnviorSupvsEntity.getQutype() != 50) {
            throw new RRException("流程状态异常");
        }
        CjEntity cjEntity = enviorSupvsDao.queryCode(pjEnviorSupvsEntity.getCode());
        if (cjEntity == null){
            throw new RRException("请选择正确的行政区名称");
        }
        //状态为驳回  则 修改状态为 第3步  将驳回信息加入驳回表
        //查询现在状态
        PjEnviorSupvsEntity pjEnviorSupvsEntity1 = enviorSupvsDao.queryInfo(pjEnviorSupvsEntity.getId());
        if (pjEnviorSupvsEntity.getQutype() == 3) {
            if (pjEnviorSupvsEntity1.getQutype() != 3){
                pjEnviorSupvsEntity.setKstime(new Date());
            }
        } else if (pjEnviorSupvsEntity.getQutype() == 4) {
            if (pjEnviorSupvsEntity1.getQutype() != 4){
                pjEnviorSupvsEntity.setXfsj(new Date());
            }
        } else if (pjEnviorSupvsEntity.getQutype() == 2) {
            if (pjEnviorSupvsEntity1.getCounts3() != null && pjEnviorSupvsEntity1.getCounts3() > 2) {
                if (ShiroUtils.getUserEntity().getType() != 1){
                    throw new RRException("本月超出提交次数");
                }
            }
            pjEnviorSupvsEntity.setCounts3((pjEnviorSupvsEntity1.getCounts3() == null ? 0 : pjEnviorSupvsEntity1.getCounts3()) + 1);
            pjEnviorSupvsEntity.setCgtime(new Date());
        }else if (pjEnviorSupvsEntity.getQutype() == 5){
            if (pjEnviorSupvsEntity.getEndtime() == null){
                pjEnviorSupvsEntity.setEndtime(new Date());
            }
        }
        PjEnviorRejectedEntity pjEnviorRejectedEntity = new PjEnviorRejectedEntity();
        //3-4
        if (pjEnviorSupvsEntity.getQutype() == 102) {
            pjEnviorSupvsEntity1.setQutype(4);
            enviorSupvsDao.update(pjEnviorSupvsEntity1);
        } else
            //5-4驳
            if (pjEnviorSupvsEntity.getQutype() == 101) {
//                if (pjEnviorSupvsEntity1.getCounts2() != null && pjEnviorSupvsEntity1.getCounts2() > 2) {
//                    throw new RRException("驳回次数到达上限", 500);
//                }
                pjEnviorSupvsEntity.setSqbh(true);
                if (pjEnviorSupvsEntity1.getZjwj() == 1) {
                    pjEnviorSupvsEntity.setZjwj(1);
                    enviorSupvsDao.updateBh(pjEnviorSupvsEntity);
                    return;
                }
                pjEnviorSupvsEntity.setCounts2((pjEnviorSupvsEntity1.getCounts2() == null ? 0 : pjEnviorSupvsEntity1.getCounts2()) + 1);
                pjEnviorRejectedEntity.setCltime(pjEnviorSupvsEntity1.getCgtime());
                pjEnviorRejectedEntity.setKsname(pjEnviorSupvsEntity1.getCgname());
                pjEnviorRejectedEntity.setKstel(pjEnviorSupvsEntity1.getCgtel());
                pjEnviorRejectedEntity.setKsbz(pjEnviorSupvsEntity1.getCgbz());
                pjEnviorRejectedEntity.setYspj(pjEnviorSupvsEntity1.getCgpj());

                //写入驳回的备注
                SysUserEntity userEntity = ShiroUtils.getUserEntity();
                pjEnviorRejectedEntity.setBhr(userEntity.getName());
                pjEnviorRejectedEntity.setBhtel(userEntity.getMobile());
                pjEnviorRejectedEntity.setBhbz(pjEnviorSupvsEntity.getRejectBz());
                pjEnviorRejectedEntity.setBhtime(new Date());
                pjEnviorRejectedEntity.setBhyj(pjEnviorSupvsEntity.getRejectYj());
                pjEnviorRejectedEntity.setEsid(pjEnviorSupvsEntity.getId());
                pjEnviorRejectedEntity.setYspj(pjEnviorSupvsEntity.getCgpj());
                pjEnviorRejectedEntity.setType(2);
                //添加驳回备注
                enviorSupvsDao.saveRejected(pjEnviorRejectedEntity);
                //修改图片到驳回图
                enviorFileService.saveRejectedFile5_4(pjEnviorRejectedEntity.getId(), pjEnviorSupvsEntity1.getId());
                enviorSupvsDao.updateBh(pjEnviorSupvsEntity);

            } else
                //4-3驳回
                if (pjEnviorSupvsEntity.getQutype() == 100) {
//                    if (pjEnviorSupvsEntity1.getCounts() != null && pjEnviorSupvsEntity1.getCounts() > 8) {
//                        throw new RRException("驳回次数到达上限", 500);
//                    }
                    pjEnviorSupvsEntity.setCounts((pjEnviorSupvsEntity1.getCounts() == null ? 0 : pjEnviorSupvsEntity1.getCounts()) + 1);
                    //将状态 3 4 添加到驳回表 并清空状态 3 4
                    SysUserEntity userEntity = ShiroUtils.getUserEntity();
                    pjEnviorRejectedEntity.setCltime(pjEnviorSupvsEntity1.getXfsj());
                    pjEnviorRejectedEntity.setKsname(pjEnviorSupvsEntity1.getKsname());
                    pjEnviorRejectedEntity.setKstel(pjEnviorSupvsEntity1.getKstel());
                    pjEnviorRejectedEntity.setKsbz(pjEnviorSupvsEntity1.getKsbz());
                    pjEnviorRejectedEntity.setBhr(userEntity.getName());
                    pjEnviorRejectedEntity.setBhtel(userEntity.getMobile());
                    pjEnviorRejectedEntity.setBhbz(pjEnviorSupvsEntity.getCgbz());
                    pjEnviorRejectedEntity.setBhtime(new Date());
                    pjEnviorRejectedEntity.setBhyj(pjEnviorSupvsEntity.getCgpj());
                    pjEnviorRejectedEntity.setEsid(pjEnviorSupvsEntity.getId());
                    pjEnviorRejectedEntity.setType(1);

                    enviorSupvsDao.saveRejected(pjEnviorRejectedEntity);
                    //修改图片到驳回图
                    enviorFileService.saveRejectedFile(pjEnviorRejectedEntity.getId(), pjEnviorSupvsEntity1.getId());
                    //删除图片
                    enviorFileService.deleteBh(pjEnviorSupvsEntity1.getId());
                    //设置处理人为空 状态变为3
                    enviorSupvsDao.updateBh(pjEnviorSupvsEntity);
                } else {
                    enviorSupvsDao.update(pjEnviorSupvsEntity);
                }
    }

    @Override
    public List<KeyValueEntity> queryByZhen(CunZhenDto cunZhenDto) {
        return enviorSupvsDao.queryByZhen(cunZhenDto);
    }

    @Override
    public List<EnviorFileFhEntity> getEnviorsByxy(PointAndTypeEntity pointAndTypeEntity) {
        //验证
        if (pointAndTypeEntity.getType() == 4) {
            if (pointAndTypeEntity.getType() == null
                    || pointAndTypeEntity.getXmax() == null
                    || pointAndTypeEntity.getXmin() == null
                    || pointAndTypeEntity.getYmax() == null) {
                throw new RRException("参数错误");
            }
        }
        if (pointAndTypeEntity.getType() == 1) {
            return enviorSupvsDao.getEnviorsByxy1(pointAndTypeEntity);
        } else if (pointAndTypeEntity.getType() == 2) {
            return enviorSupvsDao.getEnviorsByxy2(pointAndTypeEntity);
        } else if (pointAndTypeEntity.getType() == 3) {
            return enviorSupvsDao.getEnviorsByxy3(pointAndTypeEntity);
        } else if (pointAndTypeEntity.getType() == 4) {
            return enviorSupvsDao.getEnviorsByxy4(pointAndTypeEntity);
        }
        throw new RRException("type超出规定范围");

    }
}
